package com.vietnam.power.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.vietnam.power.constant.AppConfigurationConstant;
import com.vietnam.power.constant.GeneralConstant;
import com.vietnam.power.domain.Transaction;
import com.vietnam.power.domain.TransactionSearchCriteria;
import com.vietnam.power.service.FileService;
import com.vietnam.power.service.TransactionService;

@Controller
@RequestMapping("/admin")
public class TransactionController {
	private static Logger logger = Logger.getLogger(TransactionController.class);
	@Autowired
	private MessageSource messageSource;

	@Autowired
	private TransactionService transactionService;

	@Autowired
	private FileService fileService;

	@RequestMapping(value = { "/saveTransaction" }, method = RequestMethod.POST)
	public String saveTransaction(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "transaction") Transaction transaction) {

		logger.debug(transaction.getId());
		transactionService.updateTransaction(transaction);

		return "redirect:/admin/viewTransaction.html?id=" + transaction.getId();
	}

	@RequestMapping(value = { "/viewTransaction" }, method = RequestMethod.GET)
	public ModelAndView viewTransaction(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) int id) {
		ModelAndView mav = new ModelAndView("admin/newTransaction");

		mav.addObject("orderStatusList", buildOrderStatusList());
		mav.addObject("transaction", transactionService.getTransactionById(id));
		mav.addObject("mode", GeneralConstant.MODE_VIEW);
		mav.addObject("pageTitle", messageSource.getMessage("admin.transaction.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/updateTransaction" }, method = RequestMethod.GET)
	public ModelAndView updateTransaction(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) int id) {
		ModelAndView mav = new ModelAndView("admin/newTransaction");
		logger.debug("product group id: " + id);

		mav.addObject("mode", GeneralConstant.MODE_EDIT);
		mav.addObject("orderStatusList", buildOrderStatusList());
		mav.addObject("transaction", transactionService.getTransactionById(id));
		mav.addObject("pageTitle", messageSource.getMessage("admin.transaction.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchTransaction" }, method = RequestMethod.GET)
	public ModelAndView searchTransaction(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("admin/searchTransaction");

		mav.addObject("searchTransaction", new TransactionSearchCriteria());
		mav.addObject("orderStatusList", buildOrderStatusList());
		mav.addObject("pageTitle", messageSource.getMessage("admin.transaction.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchTransaction" }, method = RequestMethod.POST)
	public ModelAndView searchTransaction(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "searchTransaction") TransactionSearchCriteria s) {
		ModelAndView mav = new ModelAndView("admin/searchTransaction");

		mav.addObject("searchTransaction", s);
		mav.addObject("orderStatusList", buildOrderStatusList());
		mav.addObject("rs", transactionService.searchTransaction(s));
		mav.addObject("pageTitle", messageSource.getMessage("admin.transaction.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/checkTxnAlert" }, method = RequestMethod.POST)
	public @ResponseBody
	List<Transaction> checkTxnAlert() {
		TransactionSearchCriteria s = new TransactionSearchCriteria();
		s.setOrderStatus(AppConfigurationConstant.TransactionStatus.PENDING.getOrderStatus());
		return transactionService.searchTransaction(s);
	}
	
	private Map<Integer, String> buildOrderStatusList() {
		// build order list
		Map<Integer, String> orderStatusList = new HashMap<Integer, String>();
		orderStatusList.put(AppConfigurationConstant.TransactionStatus.PENDING.getOrderStatus(),
				messageSource.getMessage("admin.order.status.pending", null, LocaleContextHolder.getLocale()));
		orderStatusList.put(AppConfigurationConstant.TransactionStatus.PROCESSING.getOrderStatus(),
				messageSource.getMessage("admin.order.status.processing", null, LocaleContextHolder.getLocale()));
		orderStatusList.put(AppConfigurationConstant.TransactionStatus.DONE.getOrderStatus(),
				messageSource.getMessage("admin.order.status.done", null, LocaleContextHolder.getLocale()));
		orderStatusList.put(AppConfigurationConstant.TransactionStatus.CANCEL.getOrderStatus(),
				messageSource.getMessage("admin.order.status.cancel", null, LocaleContextHolder.getLocale()));

		return orderStatusList;
	}

}
